package com.ctsi.question.controller;

import com.ctsi.commons.util.UtilValidate;
import com.ctsi.commons.util.response.Page;
import com.ctsi.framework.common.response.UnifiedResponse;
import com.ctsi.framework.security.util.SecurityUtil;
import com.ctsi.organise.common.api.PersonServiceProxy;
import com.ctsi.organise.common.model.Person;
import com.ctsi.question.dto.PersonSelectDTO;
import com.ctsi.question.dto.ReportLeaderDTO;
import com.ctsi.question.entity.ReportLeader;
import com.ctsi.question.service.ReportLeaderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@RestController
public class LeaderManagerController {

    @Autowired
    private ReportLeaderService reportLeaderService;

    @Autowired
    private PersonServiceProxy personServiceProxy;

    /**
     * 人员列表
     * @param personName
     * @param parentId
     * @param start
     * @param pageSize
     * @return
     */
    @PostMapping(value = "/report-leader/leader-list")
    public UnifiedResponse<Map<String,Object>> leaderList(@RequestParam(value = "personName",required = false) String personName,
                                           @RequestParam(value = "parentId",required = false,defaultValue = "root") String parentId,
                                           @RequestParam(value = "isback",required = false,defaultValue = "0") Integer isback,
                                           @RequestParam(value = "start")Integer start,
                                           @RequestParam(value = "pageSize") Integer pageSize){
        Map<String,Object> result = new HashMap<>();
        String personNameLike = null;
        if (UtilValidate.isNotEmpty(personName)) {
            personNameLike = "%"+personName+"%";
        }
        Page<ReportLeaderDTO> page = null;
        boolean back = isback == 1;
        if (back && !parentId.equals("root")) {
            // 返回操作，通过parentId获取parent对象，通过parent对象的parentId获取数据
            ReportLeader pl = reportLeaderService.findReportLeaderById(parentId);
            parentId = pl.getParentId();
        }
        page = reportLeaderService.findReportLeaderDTOPage(personNameLike,parentId,start,pageSize);
        result.put("parentId",parentId);
        result.put("page",page);
        return UnifiedResponse.ok(result);
    }

    /**
     * 添加领导关系
     * @return
     */
    @PostMapping(value = "/report-leader/add-item")
    public UnifiedResponse<Void> addMember(@RequestParam("personId") String personId,
                                           @RequestParam(value ="jobName",required = false) String jobName,
                                           @RequestParam("parentId") String parentId){
        String userId = SecurityUtil.getUser().getUserId();
        String[] personIds = personId.split(",");
        Stream.of(personIds).forEach(pid -> {
            if (!reportLeaderService.personExit(pid,null)) {
                ReportLeader leader = new ReportLeader();
                leader.setPersonId(pid);
                Person person = personServiceProxy.findPersonById(pid);
                leader.setPersonName(person.getFullName());
                leader.setParentId(parentId);
                leader.setJobName(jobName);
                leader.setCreateBy(userId);
                leader.setCreateAt(System.currentTimeMillis());
                reportLeaderService.insert(leader);
            }
        });
        return UnifiedResponse.ok();
    }

    @PostMapping(value = "/report-leader/del-item")
    public UnifiedResponse<Void> del(@RequestParam("ids") String ids){
        String[] idss = ids.split(",");
        reportLeaderService.delMuti(Stream.of(idss).collect(Collectors.toSet()));
        return UnifiedResponse.ok();
    }


    @PostMapping(value = "/report-leader/person-select")
    public UnifiedResponse<Page<PersonSelectDTO>> personSelect(@RequestParam(value = "personNameLike",required = false) String personNameLike,
                                                               @RequestParam(value = "start") Integer start,
                                                               @RequestParam(value = "pageSize",required = false,defaultValue = "15")Integer pageSize){
        if (UtilValidate.isNotEmpty(personNameLike)) {
            personNameLike = "%"+personNameLike+"%";
        } else {
            personNameLike = null;
        }
        Page<PersonSelectDTO> result = reportLeaderService.findPersonSelect(personNameLike,start,pageSize);
        return UnifiedResponse.ok(result);
    }

}
